
package com.usersmodule.servlet;
import com.usersmodule.bean.TbModule;
import com.usersmodule.dao.TbModuleDAO;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import util.*;

public class TbModuleServlet extends HttpServlet{
	public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        String moduleName=request.getParameter("moduleName");
        int moduleSequence=Integer.parseInt(request.getParameter("moduleSequence")==null?"0":request.getParameter("moduleSequence"));
        String moduleUrl=request.getParameter("moduleUrl");
        int parentModuleId=Integer.parseInt(request.getParameter("parentModuleId"));
        int isEnable=Integer.parseInt(request.getParameter("isEnable"));
        String moduleMemo=request.getParameter("moduleMemo");
            if(new TbModuleDAO().add(new TbModule(moduleName,moduleSequence,moduleUrl,parentModuleId,isEnable,moduleMemo))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        else if("del".equals(method)){
            if(UtilDAO.delin("Tb_Module", "ModuleId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        else if("edit".equals(method)){
        int moduleId=Integer.parseInt(request.getParameter("moduleId"));
        String moduleName=request.getParameter("moduleName");
        int moduleSequence=Integer.parseInt(request.getParameter("moduleSequence")==null?"0":request.getParameter("moduleSequence"));
        String moduleUrl=request.getParameter("moduleUrl");
        int parentModuleId=Integer.parseInt(request.getParameter("parentModuleId"));
        int isEnable=Integer.parseInt(request.getParameter("isEnable"));
        String moduleMemo=request.getParameter("moduleMemo");
            if(new TbModuleDAO().edit(new TbModule(moduleId,moduleName,moduleSequence,moduleUrl,parentModuleId,isEnable,moduleMemo))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        else if ("list".equals(method)) goList(request, response);
        else if ("listById".equals(method)) {
        	//查询所有父模块
        	request.getSession(true).setAttribute("tbModuleList", new TbModuleDAO().getTbModuleAll());
            request.getSession(true).setAttribute("tbModule",new TbModuleDAO().getTbModuleByModuleId(Integer.parseInt(request.getParameter("moduleId"))));
            response.sendRedirect("UsersModule/TbModuleOperate.jsp");
    	}
        else if("showadd".equals(method)){
        	request.getSession().removeAttribute("tbModule");
            request.getSession(true).setAttribute("tbModuleList", new TbModuleDAO().getTbModuleAll());
            response.sendRedirect("UsersModule/TbModuleOperate.jsp");
        }
        else if("toindex".equals(method)){
        	request.getSession(true).setAttribute("tbModuleList",new TbModuleDAO().getTbModuleByParentModuleId(0));
        	response.sendRedirect("index.jsp");
        }
    }
	public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().setAttribute("tbModuleList",new TbModuleDAO().getTbModuleAll());
        response.sendRedirect("UsersModule/TbModuleList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
